<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search">
            <div class="search-item">
                <label>考试标题：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="考试标题"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>学校：</label>
                <el-cascader
                    ref="schoolRef"
                    :key="key"
                    v-model="queryData.schoolId"
                    :props="schoolProps"
                    :show-all-levels="false"
                    filterable
                    clearable
                    @change="handleSchool"
                ></el-cascader>
            </div>
            <div class="search-item">
                <label>专业：</label>
                <div class="table-query-input">
                    <el-cascader
                        ref="departmentRef"
                        v-model="queryData.departmentId"
                        v-loading="departmentLoading"
                        :options="departmentInfo"
                        placeholder="请选择学科"
                        :show-all-levels="false"
                        clearable
                        filterable
                        :props="{
                            value: 'id',
                            label: 'name',
                            emitPath: false,
                            checkStrictly: true
                        }"
                    >
                    </el-cascader>
                </div>
            </div>

            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                    >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="danger"
                plain
                style="margin-bottom: 2em"
                :disabled="ids.length === 0"
                @click="
                    tableDelete(
                        '/exam/exam/deleted-list',
                        {
                            ids: ids
                        },
                        true
                    )
                "
                >删除
            </el-button>
        </div>

        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    type="selection"
                    min-width="55"
                    align="center"
                    :resizable="false"
                >
                </el-table-column>
                <el-table-column
                    prop="schoolName"
                    label="学校"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="name"
                    label="作业名称"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="departmentName"
                    label="学校专业"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="classificationName"
                    label="科目"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="courseName"
                    label="课程"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="startTime"
                    label="开始时间"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="endTime"
                    label="结束时间"
                    width="160"
                ></el-table-column>
                <el-table-column prop="ruleType" label="组卷方式" width="120">
                    <template slot-scope="scope">
                        {{ ruleType[scope.row.ruleType] }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="createdTime"
                    label="创建时间"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="createdByName"
                    label="最后操作者"
                    width="160"
                ></el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    fixed="right"
                    min-width="300"
                >
                    <!--                    :disabled="scope.row.released"-->
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="toEditPg(scope.row)"
                            >编辑
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="warning"
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            :loading="scope.row.opLoading"
                            @click="publicExam(scope.row)"
                            >{{ scope.row.released ? "取消发布" : "发布" }}
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            :disabled="scope.row.released"
                            class="warning"
                            size="mini"
                            type="text"
                            icon="el-icon-circle-plus-outline"
                            :loading="scope.row.opLoading"
                            @click="clickPaperReEdit(scope.row)"
                            >关联试卷
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            type="text"
                            icon="el-icon-picture-outline"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="toPaperQuestion(scope.row)"
                            >查看试卷
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            icon="el-icon-picture-outline"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="toPaperRecord(scope.row)"
                            >查看考试记录
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            v-if="paperQuestionVisible"
            title="查看试卷"
            :visible.sync="paperQuestionVisible"
            width="80%"
        >
            <paperQuestionComponent
                :data-form="paperData"
            ></paperQuestionComponent>
            <span slot="footer" class="dialog-footer">
                <el-button @click="paperQuestionVisible = false"
                    >关 闭</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { TablePage } from "@/layout/mixin/iVue.js";
import { mapState } from "vuex";
import { treeList2Tree } from "@/utils/index.js";
import {
    fetchCourseList,
    fetchClassesAll,
    fetchClassificationFirst,
    fetchClassificationChildren
} from "@/api/course";
import { fetchPaperByExamId, releaseExam } from "@/api/exam";
import paperQuestionComponent from "@/views/models/exam/component/paperQuestionComponent";
import {
    fetchDepartmentList,
    fetchSchoolChildren,
    fetchOtherSchoolFirst
} from "@/api/user";
export default {
    name: "GroupExamWork",
    components: {
        paperQuestionComponent
    },
    mixins: [TablePage],
    data() {
        return {
            paperData: {},
            schoolProps: {
                lazy: true,
                checkStrictly: true,
                emitPath: false,
                lazyLoad: this.getSchool
            },
            ids: [],
            departmentInfo: [],
            departmentLoading: false,
            paperQuestionVisible: false,
            currentClassificationId: null,
            queryData: {
                paperType: "finalExam",
                school: "other"
            },
            initQueryData: {
                paperType: "finalExam",
                school: "other"
            },
            flag: {
                course: false
            },
            courseQueryData: {
                size: 1000,
                courseSubjectId: ""
            },
            courseInfo: [],
            initAddForm: {
                id: null,
                name: null,
                remark: null,
                actionCodes: []
            },
            classificationProps: {
                lazy: true,
                checkStrictly: true,
                emitPath: false,
                lazyLoad: this.getClassification
            },
            key: 0,
            actions: null,
            actionSearch: null,
            src:
                "https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg",
            depInfo: []
        };
    },
    computed: {
        ...mapState({
            status: state => state.status.paperType,
            joinType: state => state.status.joinType,
            ruleType: state => state.status.ruleType
        })
    },
    watch: {
        $route: function(to, from) {
            // 拿到目标参数 to.query.id 去再次请求数据接口
            if (from.name === "AddExamFinal") {
                this.search();
            }
        }
    },
    mounted() {},
    methods: {
        search() {
            this.loadTable("/exam/exam/page", this.queryData);
        },
        toEditPg(data) {
            this.$router.push({
                name: "AddExamFinal",
                query: { examId: data.id, from: true }
            });
        },
        getSchool(node, resolve) {
            const { level } = node;
            let disable = false;
            if (!node.data) {
                fetchOtherSchoolFirst().then(res => {
                    if (res !== null) {
                        const nodes = Array.from(res).map(item => {
                            disable =
                                this.currentSchoolId !== null &&
                                item.id === this.currentSchoolId;
                            return {
                                value: item.id,
                                label: `${item.name}`,
                                leaf:
                                    level >= (item.children.length > 0 ? 1 : 0),
                                code: item.code,
                                disabled: disable
                            };
                        });
                        resolve(nodes);
                    }
                });
            } else {
                fetchSchoolChildren({ id: node.data.value }).then(res => {
                    const nodes = Array.from(res).map(item => {
                        disable =
                            (this.currentSchoolId !== null &&
                                item.id === this.currentSchoolId) ||
                            this.currentSchoolId === item.parentId;
                        return {
                            value: item.id,
                            label: `${item.name}`,
                            leaf:
                                level >=
                                (item.children.length > 0 ? level + 1 : 0),
                            code: item.code,
                            disabled: disable
                        };
                    });
                    resolve(nodes);
                });
            }
        },
        //根据学校查询专业
        handleSchool(data) {
            let checkedNodes = this.$refs.schoolRef.getCheckedNodes();

            this.queryData.schoolCode = checkedNodes[0].data.code;
            this.departmentLoading = true;
            fetchDepartmentList(
                { schoolCode: this.queryData.schoolCode, type: "xklb" },
                "查询专业信息异常"
            ).then(res => {
                this.departmentInfo = treeList2Tree(res);
                this.departmentLoading = false;
            });
        },

        // getClassification(node, resolve) {
        //     const {level} = node;
        //     let disable = false;
        //     if (!node.data) {
        //         fetchClassificationFirst().then(res => {
        //             if(res !== null){
        //                 const nodes = Array.from(res).map(item => {
        //                     disable = this.currentClassificationId !== null && item.id === this.currentClassificationId;
        //                     return {
        //                         value: item.id,
        //                         label: `${item.name}`,
        //                         leaf: level >= (item.children.length > 0 ? 1 : 0),
        //                         disabled:disable
        //                     }
        //                 })
        //                 resolve(nodes)
        //             }
        //         })
        //     } else {
        //         fetchClassificationChildren({id: node.data.value}).then(res => {
        //             const nodes = Array.from(res).map(item => {
        //                 disable = (this.currentClassificationId !== null && item.id === this.currentClassificationId) ||  this.currentClassificationId === item.parentId;
        //                 return {
        //                     value: item.id,
        //                     label: `${item.name}`,
        //                     leaf: level >= (item.children.length > 0 ? (level + 1) : 0),
        //                     disabled:disable
        //                 }
        //             })
        //             resolve(nodes)
        //         })
        //     }
        // },
        publicExam(data) {
            data.opLoading = true;
            releaseExam({ id: data.id, released: !data.released }, "发布异常")
                .then(res => {
                    if (res) {
                        data.opLoading = false;
                        this.search();
                    }
                })
                .finally(() => {
                    data.opLoading = false;
                });
        },
        handleSelectionChange(val) {
            this.ids = [];
            const length = val.length;
            for (let n = 0; n < length; n++) {
                this.ids.push(val[n].id);
            }
        },
        //编辑考试-试卷关联关系
        clickPaperReEdit(data) {
            this.$router.push({
                name: "ExamPaperRe",
                query: {
                    examId: data.id,
                    ruleType: data.ruleType,
                    paperType: data.paperType,
                    schoolCode: data.schoolCode,
                    school: "other",
                    classificationId: data.classificationId
                }
            });
        },
        toPaperQuestion(data) {
            //查询关联试卷
            fetchPaperByExamId({ id: data.id }, "查询关联试卷异常").then(
                res => {
                    this.paperData = res;
                    this.paperQuestionVisible = true;
                }
            );
        },
        toPaperRecord(data) {
            this.$router.push({
                name: "PaperRecord",
                query: { examId: data.id, paperType: data.paperType }
            });
        }
    }
};
</script>

<style lang="scss" scoped>
.container {
    background: #fff;
    border-radius: 0.3em 0.3em 0 0;
    padding-bottom: 1em;
    padding-top: 1em;
    padding-left: 1em;
    margin-top: 16px;

    .container-content {
        padding-top: 12px;

        p {
            color: #909399;
            font-size: 15px;
            padding-top: 12px;
        }
    }

    .container-right {
        margin-left: 12px;
        margin-top: 2em;

        .r-img {
            max-width: 240px;
            max-height: 160px;
        }

        .r-btn {
            margin-top: 22px;
        }
    }
}

.table-search-box {
    padding-left: 17px;
    font-size: 16px;
}
</style>
